<?php

namespace app\manage\controller;

use app\common\controller\Manage;
use think\Db;

class PayBill extends Manage
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if($this->request->isAjax()) {
            $page = $this->request->get('page', 1);
            $limit = $this->request->get('limit', 15);
            $condition = [];
            if($this->request->get('bill_bn')) {
                $condition[] = ['bill_bn', '=', $this->request->get('bill_bn')];
            }
            if($this->request->get('date')) {
                $condition[] = ['FROM_UNIXTIME(ctime,"%Y-%m-%d")', '=', $this->request->get('date')];
            }
            switch ($this->request->get('type')) {
                case 'no':
                    $condition[] = ['status', 'eq', '0'];
                    break;
                default :
                    
                    break;
            }
            $payBillModel = new \app\common\model\PayBill();
            $pay_bill_list = $payBillModel::with(['user'])->where($condition)->order('id', 'desc')->limit(($page-1)*$limit, $limit)->select();
            $pay_bill_count = $payBillModel->where($condition)->count();
            foreach ($pay_bill_list as $info) {
                $info->iscancel = ($info->pay_status == '1' && $info->ctime+86400 > time())? true:false;
            }
            return [
                'code' => 0,
                'count' => $pay_bill_count,
                'data' => $pay_bill_list
            ];
        }
        return $this->fetch();
    }
    
    public function cancel()
    {
        $payBillModel = new \app\common\model\PayBill();
        $pay_bill_info = $payBillModel->get($this->request->post('id'));
        if($pay_bill_info->status == '1' && $pay_bill_info->ctime+86400 > time()) {
            $balanceModel = new \app\common\model\Balance();
            Db::startTrans();
            $res = $balanceModel->change($pay_bill_info->user_id, 2, $pay_bill_info->amount, $pay_bill_info->id);
            if($res['status']) {
                $res = $pay_bill_info->isUpdate()->save(['status' => 2], ['id' => $pay_bill_info->id]);
                if($res) {
                    Db::commit();
                    return [
                        'status'    =>  true,
                        'msg'       =>  '取消成功'   
                    ];
                }
                Db::rollback();
                return [
                    'status'    =>  false,
                    'msg'       =>  '订单取消失败'
                ];
            } else {
                return [
                    'status'    =>  false,
                    'msg'       =>  $res['msg']
                ];
            }
        } else {
            return [
                'status'    =>  false,
                'msg'       =>  '订单无法取消'
            ];
        }
    }
    
    public function getPayBill()
    {
        $payBillModel = new \app\common\model\PayBill();
        $pay_info = $payBillModel->get($this->request->post('id'));
        $user_info = model('User')->get($pay_info->user_id);
        $pay_info->user = $user_info;
        $pay_info['ctime2'] = date('Y-m-d H:i:s', $pay_info['ctime']);
        return [
            'status' => true,
            'data' => $pay_info 
        ];
    }
    
    public function update()
    {
        $payBillModel = new \app\common\model\PayBill();
        return $payBillModel->where('id', $this->request->post('id'))->setField(['status' => 1]);
    }
}
